//安装依赖 npm install art-template --save
var template = require("art-template");
var http = require("http");
var fs = require("fs");
/** 简单使用*/
/* var tempStr = `
	<h2>{{titel}}</h2>
	<h4>{{name}}</h4>
	<ul>
		{{each likes}}
		<li>{{$value.name}}:{{$value.time}}</li>
		{{/each}}
	</ul>`;
var data = {
	'titel':'测试',
	'name':'张三',
	'likes':[
		{'name':'跑步','time':'2019-09-09'},
		{'name':'游泳','time':'2019-09-19'}
	]
};
var htmlStr = template.render(tempStr,data);
console.log(htmlStr); */

/**结合 html 页面使用模板*/

var server = http.createServer().listen(12345,function(){
	console.log("服务启动成功");
});
//监听request请求
server.on("request",function(req,res){
	var url = req.url;
	if(url === "/temp"){
		fs.readFile("./template/template.html",function(err,html){
			if(err){
				return console.error("读取模板异常",err);
			}
			var data = {
				'title':'模板测试',
				'name':'张三',
				'likes':[
					{'name':'跑步','time':'2019-09-09'},
					{'name':'游泳','time':'2019-09-19'}
				]
			};
			console.log(html.toString());//读出来的数据一定要转字符串
			var htmlStr = template.render(html.toString(),data);
			res.end(htmlStr);
		});
	}else{
		console.log("404");
		res.end("404");
	}
});